package com.hk.servlet;

import com.alibaba.fastjson.JSONObject;
import com.hk.dao.INewsDao;
import com.hk.dao.impl.NewsDaoImpl;
import com.hk.model.*;
import com.hk.model.vo.ResultVO;
import com.hk.service.*;
import com.hk.service.impl.*;
import com.hk.utils.CharacterEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: KangWang
 * Date: 2021/5/17 10:20
 * State：Servlet 数据请求转发 控制层
 */
@WebServlet("/home")
public class HomeServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        统一字符编码
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
//        获取热门新闻
        INewsService iNewsService = new NewsServiceImpl();
//        查询浏览次数大于并=600的新闻
        List<News> newsList = iNewsService.findByHits(600);
//        获取产品图片信息
        IProductService iProductService = new ProductServiceImpl();
        List<Product> productList = iProductService.findAllImg(-1, -1);
        Map<String,Object> map = new HashMap<>();
        map.put("status",0);
        map.put("message","success");
        map.put("productList",productList);
        map.put("newsList",newsList);
//        System.out.println("JSON="+resultVO);
        String mapJSON = JSONObject.toJSONString(map);
        PrintWriter out = resp.getWriter();
        out.write(mapJSON);
        out.flush();
        out.close();
    }

}
